﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace ParsingBot.Naver
{
	class Naver_Manager
	{
		private string GetParam(string input, string param)	//임시
		{
			return input.Split( (param+"=").ToCharArray())[1].Split('&')[0];
		}
		
		public List<CartoonSimple> GetCartoonSimpleList()	//만화 전체 목록 가져옴
		{
			string url = "http://comic.naver.com/webtoon/creation.nhn?view=image";
			HtmlWeb web = new HtmlWeb();
			HtmlDocument doc = web.Load(url);
			HtmlNodeCollection hnc = doc.GetElementbyId("content").SelectSingleNode("div[@class='all_list all_image']").SelectNodes("div[@class='section']/ul/li");
			
			List<CartoonSimple> lCartoonSimple = new List<CartoonSimple>(hnc.Count);
			CartoonSimple cartoonSimple;
			
			foreach (HtmlNode n in hnc)
			{
				cartoonSimple  = new CartoonSimple();
				if(n.SelectSingleNode("div/img") == null)
					cartoonSimple.Complete = false;
				else
					cartoonSimple.Complete = true;

				cartoonSimple.Name = n.SelectSingleNode("div/a").Attributes["title"].Value;
				cartoonSimple.titleId = n.SelectSingleNode("div/a").Attributes["href"].Value.Split('=')[1];
				cartoonSimple.Thumbnail = n.SelectSingleNode("div/a/img").Attributes["src"].Value;

				lCartoonSimple.Add(cartoonSimple);
			}
			return lCartoonSimple;
		}
		
		public CartoonInfo GetCartoonInfo(string titleId)	//만화의 제목, 이미지, 작가, 설명 가져옴
		{
			string url = "http://comic.naver.com/webtoon/list.nhn?titleId=" + titleId;
			HtmlWeb web = new HtmlWeb();
			HtmlDocument doc = web.Load(url);
			CartoonInfo cartoonInfo = new CartoonInfo();
			HtmlNode h;
			
			h = doc.GetElementbyId("content").SelectSingleNode("div[@class='comicinfo']");
			cartoonInfo.Image = h.SelectSingleNode("div/a/img").Attributes["src"].Value;
			cartoonInfo.Title = h.SelectSingleNode("div[@class='detail']/h2").InnerText;
			cartoonInfo.Author = h.SelectSingleNode("div[@class='detail']/strong").InnerText.Replace("\t", "").Replace("\n", "").Replace("\r", "").Trim();
			//Todo: 설명 가져오는 부분 작업해야함!!

			return cartoonInfo;
		}
		
		public List<EpisodeInfo> GetEpisodeList(string titleId, int page)	//해당 페이지의 만화 에피소드 목록 가져옴
		{
			string url = "http://comic.naver.com/webtoon/list.nhn?titleId=" + titleId + "&page=" + page.ToString();
			HtmlWeb web = new HtmlWeb();
			HtmlDocument doc = web.Load(url);
			HtmlNodeCollection hnc = doc.GetElementbyId("content").SelectNodes("table/tr");
			hnc.Remove(0);
			hnc.Remove(0);
			
			List<EpisodeInfo> lEpsInfo = new List<EpisodeInfo>(hnc.Count);
			EpisodeInfo epsInfo = new EpisodeInfo(); 
			foreach (HtmlNode n in hnc)
			{
				epsInfo.No = GetParam(n.SelectSingleNode("td/a").Attributes["href"].Value, "no");
				epsInfo.Thumbnail = n.SelectSingleNode("td/a/img").Attributes["src"].Value;
				epsInfo.Title = n.SelectSingleNode("td/a/img").Attributes["title"].Value;
				epsInfo.Score = float.Parse(n.SelectSingleNode("td/div/strong").InnerText);
				//epsInfo.UpdateDate = DateTime.Parse(n.SelectSingleNode("td[@class='num']").InnerText);
				//Todo: 날짜 변환 필요
				lEpsInfo.Add(epsInfo);
			}

			return lEpsInfo;
		}
		
		public Episode GetEpisode(string titleId, string no)	//해당 에피솓의 내용 가져옴
		{
			string url = "http://comic.naver.com/webtoon/detail.nhn?titleId=" + titleId + "&no=" + no.ToString();
			HtmlWeb web = new HtmlWeb();
			HtmlDocument doc = web.Load(url);
			int st,fn;
			string html;
			string imgList;
			string[] images;
			Episode epi = new Episode();
			HtmlNode h = doc.GetElementbyId("content").SelectSingleNode("div/div[@class='tit_area']");
			epi.Title = h.SelectSingleNode("div[@class='view']/h3").InnerText;
			epi.Score = float.Parse( doc.GetElementbyId("topPointTotalNumber").SelectSingleNode("strong").InnerText );
			epi.UpdateDate = DateTime.Parse( h.SelectSingleNode("div/dl[@class='rt']/dd[@class='date']").InnerText );

			html = doc.DocumentNode.InnerHtml;
			st = html.IndexOf("var imageList = [");
			fn = html.IndexOf("var imageWidth = [");
			imgList = html.Substring(st + "var imageList = [".Length, fn - st - "var imageList = [".Length);
			imgList = imgList.Trim().Replace("[", "").Replace("\\", "").Replace("\"", "");
			images = imgList.Split(',');
			epi.Images = images.ToList();
			
			return epi;	
		}
	}
}
